<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class limgcontroller extends Controller
{
    //轮播图
    //add
    public function add()
    {
    	$data = DB::table('goods') ->get();
    	// dd($data);
    	return view('admin.img.add',['data' => $data]);
    }
    public function insert(Request $request)
    {
    	 $data = $request -> except('_token');
    	 // dd($data);
    	 //上传图片
    	 //判断是否上传
    	 // dd($request -> hasFile('img'));
    	 if($request -> hasFile('img'))

    	 {
    	 	 //判断是否有效
    	 	 if($request -> file('img') -> isValid())
    	 	 {
    	 	 	//获取扩展名称
    	 	 	$suffix = $request -> file('img') -> getClientOriginalExtension();
    	 	 	//随机一个文件名称
    	 	 	$fileName = time().mt_rand(100000,999999).'.'.$suffix;
    	 	 	//移动图片
    	 	 	$request -> file('img') -> move('./uploads',$fileName);
    	 	 	//处理img字段
    	 	 	$data['img'] = '/uploads/'.$fileName;
    	 	 }else{
    	 	 	return back() -> with(['info' => '图片有问题']);
    	 	 }
    	 }else{
    	 	 return back() -> with(['info' => '请上传图片']);
    	 }
         // date_default_timezone_set('PRC');
         // $data['time'] = date("Y-m-d H:i:s");
        // dd($data);
        //执行查询
        $good = DB::table('goods') -> select('goods.name as gname','goods.img as limg','goods.sid','goods.id as gid') -> where('id',$request -> id) ->first();
        // dd($good);
        //执行添加数据
        $res = DB::table('imgs')
        	-> insert(['gname' => $good -> gname,'limg' => $data['img'],'gid' => $good -> gid,'sid' => $good -> sid]);
            if($res)
            {
            	// return 1;
            	return redirect('admin/img/menu') -> with(['info' => '添加成功']);
            }else{
            	// return 2;
            	return back() -> with(['info' => '添加失败']);
            }
    }
    //menu
    public function menu(Request $request)
    {
    	$num = $request ->input('num',10);

     	$data = DB::table('imgs') 
     	     ->where(function($query)use($request){
     	     	$query->where('gname','like','%'.$request->input('keyword').'%');
     	     })
     	     
      	     -> paginate($num);
      	     // dd($data);
              //dd($request -> all());
     	return view('admin.img.menu',['data'=>$data,'request'=> $request -> all()]);
    }
    //alter
    public function alter($id)
    {
    	$data = DB::table('imgs')-> where('id',$id)-> first();
    	$goods = DB::table('goods') -> get();
      //dd($data);
       return view('admin.img.alter',['data' => $data,'goods' => $goods]);
    }
    //update
    public function update(Request $request)
    {
    	 $data = $request -> except('_token');
    	 // dd($data);
    	 //上传图片
    	 //判断是否上传
    	 if($request -> hasFile('img'))

    	 {
    	 	 //判断是否有效
    	 	 if($request -> file('img') -> isValid())
    	 	 {
    	 	 	//获取扩展名称
    	 	 	$suffix = $request -> file('img') -> getClientOriginalExtension();
    	 	 	//随机一个文件名称
    	 	 	$fileName = time().mt_rand(100000,999999).'.'.$suffix;
    	 	 	//移动图片
    	 	 	$request -> file('img') -> move('./uploads',$fileName);
    	 	 	//处理img字段
    	 	 	
    	 	 	$data['img'] = '/uploads/'.$fileName;
    	 	 	// dd($data['img']);
    	 	 }else{
    	 	 	return back() -> with(['info' => '图片有问题']);
    	 	 }
    	 }else{
    	 	 return back() -> with(['info' => '请上传图片']);
    	 }
    	 $good = DB::table('goods') -> select('goods.name as gname','goods.img as limg','goods.sid','goods.id as gid') -> where('id',$request -> id) ->first();
    	 //执行修改数据
        $res = DB::table('imgs')
        	->where('id',$request -> iid)
        	-> update(['gname' => $good -> gname,'limg' => $data['img'],'gid' => $good -> gid,'sid' => $good -> sid]);
    	// 判断
        if($res)
        {
        	return redirect('admin/img/menu') -> with(['info' => '修改成功']);
        }else{
        	return back() -> with(['info' => '修改失败']);
        }
    }

    //delete
    public function delete($id)
    {
    	$res = DB::table('imgs') -> where('id',$id) -> delete();

    	// 判断
        if($res)
        {
        	return redirect('admin/img/menu') -> with(['info' => '删除成功']);
        }else{
        	return back() -> with(['info' => '删除失败']);
        }
    }
}
